# Defining simple function
# 定义简单函数

# def add(x, y):
#     z = x + y
#     return z
#
#
# a = add(2, 3)
# print(a)  # 5
#
# q = add(23, 19)
# print(q)  # 42
# 函数定义以单词“def”开头，后面是函数的名称（在我们的例子中是“add”），后面是一对括号中的参数列表，后面是冒号“：”。
# 然后将函数的主体向右缩进。但对于函数的所有行都必须相同。
# 当我们停止缩进并在第一列上启动一个新的表达式时，这就说明Python函数定义已经结束了


# Passing positional parameters to a function

# 第一种：向函数传递位置参数
# def sendmail(From, To, Subject, Content):
#     print('From:', From)
#     print('To:', To)
#     print('Subject:', Subject)
#     print('')
#     print(Content)
#
# # 调用这个函数,对应位置传递参数
# sendmail('wangxin@szabgab.com', 'bob@gmail.com', '你好呀', '内容内容')


# Function parameters can be named
# 通过参数名进行传参

# def sendmail(From, To, Subject, Content):
#     print('From:', From)
#     print('To:', To)
#     print('Subject:', Subject)
#     print('')
#     print(Content)
#
#
#
# sendmail(
#     Subject='self message',
#     Content='Has some content too',
#     From='gabor@szabgab.com',
#     To='szabgab@gmail.com',
# )

# Mixing positional and named parameters
# 会报错
# def sendmail(From, To, Subject, Content):
#     print('From:', From)
#     print('To:', To)
#     print('Subject:', Subject)
#     print('')
#     print(Content)
#
# sendmail(
#     Subject = 'self message',
#     Content = 'Has some content too',
#     To = 'szabgab@gmail.com',
#     'gabor@szabgab.com',
# )

# def sendmail(From, To, Subject, Content):
#     print('From:', From)
#     print('To:', To)
#     print('Subject:', Subject)
#     print('')
#     print(Content)
#
#
# sendmail(
#     'gabor@szabgab.com',
#     Subject='self message',
#     Content='Has some content too',
#     To='szabgab@gmail.com',
# )

# Default values, optional parameters,
# 默认值，可选参数，

def prompt(question, retry=3):
    print(question)
    print(retry)

    # while retry > 0:
    #     inp = input('{} ({}): '.format(question, retry))
    #     if inp == 'my secret':
    #         return True
    #     retry -= 1
    #     return False


# prompt("Type in your password")
#
# prompt("Type in your secret", 1)
# prompt("Hello", retry=7)
# prompt(retry=42, question="Is it you?")

# Default value in first param
# def add(y,x=2):
#     print("OK")
#
# add(1)

# Several defaults, using names
# 几个默认值，需使用名称
#
# def f(a, b=2, c=3):
#     print(a, b, c)
#
#
# f(1)  # 1 2 3
# f(1, b=0)  # 1 0 3
# f(1, c=0)  # 1 2 0
# f(1, c=0, b=5)  # 1 5 0
#
# # f(b=0, 1)
# # would generate:
# # SyntaxError: non-keyword arg after keyword arg
#
# f(b=0, a=1)  # 1 0 3


# def f(a = 2, b):
#     print(a)
#     print(b)

# Arbitrary number of arguments *
# 任意数量的参数*
# The values arrive as tuple.
# 这些值作为元组，被传入。
# def mysum(*numbers):
#     print(numbers)
#     print(type(numbers))
#     total = 0
#     for s in numbers:
#         total += s    # total = total + s
#     return total

# print(mysum(1))
# print(mysum(1, 2))
# print(mysum(1, 1, 1))
# print(mysum(1, 1, 1,2,3,4,5,67,8))
#
# x = [2, 3, 5, 6]

# print(mysum(*x))

# mysum(x) # 会报错

# Fixed parmeters before the others
# def mysum(op, *numbers):
#     print(numbers)
#     if op == '+':
#         total = 0
#     elif op == '*':
#         total = 1
#     else:
#         raise Exception('invalid operator {}'.format(op))
#
#     for s in numbers:
#         if op == '+':
#             total += s
#         elif op == '*':
#             total *= s  # total = total * s
#     return total
#

# print(mysum('+', 1))
# print(mysum('+', 1, 2))
# print(mysum('*', 1, 2, 3))
# print(mysum('+', 1, 1, 3))
# print(mysum('*', 1, 1, 0))

# Arbitrary key-value pairs in parameters **
# 关键字参数

# def f(**kw):
#     print(kw)
#     print(type(kw))
#
#
# f(a=23, b=12)

# def f(name, **kw):
#     print(name)
#     print(kw)
#     print(type(kw))


# f("Foo", a=23, b=12)
# f(name="Foo", a=23, b=12)

# Every parameter option
# 各种参数
# def f(op, count=0, *things, **kw):
#     print(op)
#     print(count)
#     print(things)
#     print(kw)
#
#
# f(2, 3, 4, 5,213,333, a=23, b=12,c=34,g=123)
# print(min(1, 2, 3, 0, -1))


# Duplicate declaration of functions (multiple signatures)
# 函数的重复声明（多个签名）
#
# def add(x):
#     return x + x
#
# def add(x, y):
#     return x * y
#
# # print(add(2))  # 4
#
# print(add(2, 3))
# # TypeError: add() takes exactly 1 argument (2 given)
# # add(2)

# Return more than one value
# 多个返回值
# def calc(x, y):
#     a = x + y
#     b = x * y
#     return a, b

#
# z, q = calc(2, 3)
# print(z)
# print(q)
#
# t = calc(2, 3)
# z, q = t
# print(t)
# print(type(t))
# print(z)
# print(q)

# def xxx(a, b=2, *args, **kw):  # 位置参数，默认参数，任意长度的参数，还有关键字参数
#     print('函数体')
#     return a, b  # 可以返回多个


# 求一个数的阶乘 n!
# 1                  =  1
# 2 1*2              =  2 * 1!
# 3 1*2*3            =  3 * 2!
# 4 1*2*3*4          =  4 * 3!
# ...
# n-1 1*2*.(n-1)     =  (n -1) * (n-2)!
# n 1*2*...(n-1)*n   =  n * (n-1)!

# def f(n):
#     # 计算n!
#     if n == 1:
#         return 1
#     return n * f(n-1)
#
#
# # print(f(1))
# # print(f(2))
# # print(f(3))
# print(f(7))

# 斐波那契数列  走势有点和指数型 f(x) = x^2
# f(1) = 1
# f(2) = 1
# f(3) = f(2) + f(1) = 2
# f(4) = f(3) + f(2) = 2 + 1 =3
# ....
# f(n)  = f(n-1)+f(n-2)

# def fib(n):
#     if n == 1:
#         return 1
#     if n == 2:
#         return 1
#     return fib(n - 1) + fib(n - 2)
#
# print(fib(40))
# Non-recursive Fibonacci

# 循环实现斐波那契数列
# def fib(n):
#     if n == 1:
#         return [1]
#     if n == 2:
#         return [1, 1]
#     fibs = [1, 1]
#     for i in range(2, n):
#         fibs.append(fibs[-1] + fibs[-2])
#     return fibs
#
#
# # print(fib(1))  # [1]
# # print(fib(2))  # [1, 1]
# # print(fib(3))  # [1, 1, 2]
# print(fib(10))  # [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
# # print(fib(25))  # [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]


# Unbound recursion
# 无限递归
# 为了保护我们不受无限递归的影响，Python限制了递归的深度：

# def recursion(n):
#     # if n == 200:
#     #     return None
#     print(f"In recursion {n}")
#     recursion(n+1)
#
# recursion(1)

# Variable assignment and change - Immutable
# 变量分配和更改-不可变的

# a = 42  # number or string
# b = a  # This is a copy
# print(a)  # 42
# print(b)  # 42
# a = 1
# print(a)  # 1
# print(b)  # 42

# a = (1, 2)  # tuple
# b = a  # this is a copy
# print(a)  # (1, 2)
# print(b)  # (1, 2)
# # a[0] = 42 # TypeError: 'tuple' object does not support item assignment
# a = (3, 4, 5)
# print(a)
# print(b)

# a = [1, 2]  # list
# b = a  # b和a都指向了[1,2]
# print(a)  # [1, 2]
# print(b)  # [1, 2]
# a[0] = 42 # TypeError: 'tuple' object does not support item assignment
# print(a)  # [42,2]
# print(b)  # [42,2]


# Variable assignment and change - Mutable
# 可变的变量

# a = [5, 6]
# b = a  # this is a copy of the *reference* only
# # if we change the list in a, it will
# # change the list connected to b as well
# print(a)  # [5, 6]
# print(b)  # [5, 6]
# a[0] = 1
# print(a)  # [1, 6]
# print(b)  # [1, 6]

# a = {'name': 'Foo'}
# b = a  # this is a copy of the *reference* only
# # if we change the dictionary in a, it will
# # change the dictionary connected to b as well
# print(a)  # {'name' : 'Foo'}
# print(b)  # {'name' : 'Foo'}
# a['name'] = 'Jar Jar'
# print(a)  # {'name' : 'Jar Jar'}
# print(b)  # {'name' : 'Jar Jar'}

# Parameter passing of functions
# 函数参数传递

# x = 3
#
#
# def inc(n):
#     n += 1
#     return n
#
#
# print(x)  # 3
# print(inc(x))  # 4
# print(x)  # 3

# Passing references
# numbers = [1, 2, 3]
# def update(x):
#     x[0] = 23
# def change(numbers):
#     y = [5, 6]
#     return y
#
# print(numbers)  # [1, 2, 3]
#
# update(numbers)
# print(numbers)  # [23, 2, 3]
#
# print(change(numbers))  # [5, 6]
# print(numbers)  # [23, 2, 3]


# Function documentation
# def f(name):
#     """
#     The documentation  这是f的说明书，
#     should have more than one lines. 你要仔细看
#     """
#     print(name)
#
#
# # f("hello")
# # print(f.__doc__)
#
# # print(print.__doc__)
# # 获取说明文档
# # help(print)
# 交互式命令下可以使用help(math) help(math.sqrt)
# import math

# help(math)
# help(math.sqrt)


# a = [2, 3, 93, 18]
# b = [27, 81, 11, 35]
# c = [32, 105, 1]
#
# total_a = 0  # 这个是存储a的和
#
# for num in a:
#     # total_a = total_a + num
#     total_a += num
#     # print(total_a)
#
# avg_a = total_a / len(a)  # 存储a的平均值，和除以a个数
# print('a列表的和是{}，平均值是{}'.format(total_a, avg_a))
#
#
# total_b = 0  # 这个是存储b的和
# for num in b:
#     # total_a = total_b + num
#     total_b += num
#     # print(total_b)
#
# avg_b = total_b / len(b)  # 存储b的平均值，和除以b个数
# print('b列表的和是{}，平均值是{}'.format(total_b, avg_b))
#
#
# total_c = 0  # 这个是存储c的和
# for num in c:
#     # total_c = total_c + num
#     total_c += num
#     # print(total_c)
#
# avg_c = total_c / len(c)  # 存储c的平均值，和除以c个数
# print('c列表的和是{}，平均值是{}'.format(total_c, avg_c))

# a = [2, 3, 93, 18]
# b = [27, 81, 11, 35]
# c = [32, 105, 1]
#
#
# def my_func(num_list):
#     """
#     返回一个列表的合计和平均值
#     :param num_list: list 需要求的对象
#     :return: 合计和平均值
#     """
#     total_num = 0  # 这个是存储num_list的和
#     for num in num_list:
#         total_num += num
#
#     avg_num = total_num / len(num_list)
#     return total_num, avg_num
#

#
#
# total_a, avg_a = my_func(a)
# print('a列表的和是{}，平均值是{}'.format(total_a, avg_a))
#
# total_b, avg_b = my_func(b)
# print('b列表的和是{}，平均值是{}'.format(total_b, avg_b))
#
# total_c, avg_c = my_func(c)
# print('c列表的和是{}，平均值是{}'.format(total_c, avg_c))
#
# help(my_func)


# print(type(num_dict))

# print('{key}列表的和是{}，平均值是{}'.format(total_c, avg_c))  字典键
# my_func   # 值给了my_func

#
# num_dict = {
#     'a': [2, 3, 93, 18],
#     'b': [27, 81, 11, 35],
#     'c': [32, 105, 1],
#     'a1': [2, 3, 93, 18],
#     'b1': [27, 81, 11, 35],
#     'c3': [32, 105, 1],
#     'c4': [32, 105, 1],
#     'c5': [32, 105, 1],
#     'c6': [32, 105, 1],
#     'c7': [32, 105, 1, 123, 123, 123, 12, 12, 3123, 123, 123, 123],
# }
#
# for k_name, v_num_list in num_dict.items():
#     total_v, avg_v = my_func(v_num_list)
#     # print(k_name, v_num_list, total_v, avg_v)
#     print('{}列表的和是{}，平均值是{}'.format(k_name, total_v, avg_v))
#

# 名词解释-回文（Palindrome）：正着念和反着念一样，比如“mom”,"level" "noon" "abba"
# abab
def is_palindrome(word):
    """
    用递归的方式判断word是不是回文
    :param word: str 要判断字符串
    :return: bool True代表是，False代表否
    """
    if word == '':
        return True

    if word[0] == word[-1]:
        return is_palindrome(word[1:-1])
#
#     return False
#
# # print(is_palindrome('abab'))
# print(is_palindrome('level'))
# print(is_palindrome('noon'))


def iter_palindrome(word):

    for i in range(int(len(word) / 2)):
        if word[i] != word[-(i + 1)]:
            return False
    return True

# print(iter_palindrome('level'))
print(iter_palindrome('levea'))
